<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>工时审核（请仔细审核）</span>
      </div>
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="人员名称" prop="nickName">
          <el-input
            v-model="queryParams.nickName"
            placeholder="请输入人员"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
          <el-button
            type="success"
            plain
            icon="el-icon-check"
            size="mini"
            @click="handleBatchPass"
            >批量审核</el-button
          >
        </el-form-item>
      </el-form>

      <el-table
        v-loading="loading"
        :data="checkList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="30"> </el-table-column>
        <el-table-column label="申报编号" align="center" prop="fillId" />
        <el-table-column label="申报人员" align="center" prop="nickName" />
        <el-table-column label="项目编码" align="center" prop="projectCode" />
        <el-table-column label="项目阶段" align="center" prop="stageName" />
        <el-table-column label="事务种类" align="center" prop="officeName" />
        <el-table-column
          label="申报时间"
          align="center"
          prop="workTime"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.workTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="申报工时" align="center" prop="workHour" />
        <el-table-column label="审核状态" align="center" prop="fillStateName" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="180"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
              >详情</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-check"
              v-hasPermi="['work:check:pass']"
              @click="handlePass(scope.row)"
              >通过</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-close"
              v-hasPermi="['work:check:rollback']"
              @click="handleRollback(scope.row)"
              >回退</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 查看 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="人员姓名" prop="nickName">
          <el-input v-model="form.nickName" />
        </el-form-item>
        <el-form-item label="项目编码" prop="projectCode">
          <el-input v-model="form.projectCode" />
        </el-form-item>
        <el-form-item label="项目阶段" prop="stageName">
          <el-input v-model="form.stageName" />
        </el-form-item>
        <el-form-item label="事务种类" prop="officeName">
          <el-input v-model="form.officeName" />
        </el-form-item>
        <el-form-item label="申报时间" prop="workTime">
          <el-date-picker
            v-model="form.workTime"
            type="datetime"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="申报工时" prop="workHour">
          <el-input v-model="form.workHour" />
        </el-form-item>
        <el-form-item label="申报备注" prop="workRemark">
          <el-input v-model="form.workRemark" type="textarea" />
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 回退 -->
    <el-dialog
      :title="rollbackTitle"
      :visible.sync="isRollback"
      width="500px"
      append-to-body
    >
      <el-form
        ref="rollbackForm"
        :model="rollbackForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="回退原因" prop="rollback">
          <el-input type="textarea" :rows="3" v-model="rollbackForm.rollback" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="rollbackSubmitForm">确 定</el-button>
        <el-button @click="rollbackCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCheck, pass, batchPass, rollback } from '@/api/work/check'
export default {
  name: 'Check',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 审核列表
      checkList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: undefined
      },
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 弹出层标题
      rollbackTitle: '',
      // 是否显示弹出层
      isRollback: false,
      // 回退表单参数
      rollbackForm: {},
      // 表单校验
      rules: {
        rollback: [
          { required: true, message: '回退原因不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询审核列表 */
    getList() {
      this.loading = true
      listCheck(this.queryParams).then((response) => {
        this.checkList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 多选框 */
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    /** 详情 */
    handleView(row) {
      this.form = {}
      this.resetForm('form')
      let copy = Object.assign({}, row) //row是所编辑本行内数据对象
      this.form = copy
      this.open = true
      this.title = this.form.nickName + '申报信息'
    },
    /** 批量通过 */
    handleBatchPass() {
      if (this.multipleSelection && this.multipleSelection.length > 0) {
        const batchData = JSON.stringify({ fills: this.multipleSelection })
        batchPass(batchData).then((response) => {
          this.$modal.msgSuccess('审核成功')
          this.getList()
        })
      } else {
        this.$message.error('请勾选审核数据。。。')
      }
    },
    /** 通过 */
    handlePass(row) {
      row.params = undefined
      pass(row).then((response) => {
        this.$modal.msgSuccess('审核成功')
        this.getList()
      })
    },
    /** 回退弹框 */
    handleRollback(row) {
      this.rollbackForm = {}
      this.resetForm('rollbackForm')
      let copy = Object.assign({}, row) //row是所编辑本行内数据对象
      this.rollbackForm = copy
      this.isRollback = true
      this.rollbackTitle = '回退' + this.rollbackForm.nickName + '申报'
    },
    /** 回退确定 */
    rollbackSubmitForm() {
      if (this.rollbackForm) {
        this.$refs['rollbackForm'].validate((valid) => {
          if (valid) {
            this.rollbackForm.params = undefined
            rollback(this.rollbackForm).then((response) => {
              this.$modal.msgSuccess('回退成功')
              this.isRollback = false
              this.getList()
            })
          }
        })
      } else {
        this.$message.error('数据异常')
      }
    },
    /** 回退取消 */
    rollbackCancel() {
      this.isRollback = false
      this.rollbackForm = {}
      this.multipleSelection = ''
    }
  }
}
</script>

<style></style>
